import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Vector;

public class MovieAppGUI extends JFrame {
	private JTextField movieTypeField;
	private JButton searchButton;
	private JComboBox<String> filmTypeComboBox;
	private JButton loginButton;

	private DefaultTableModel tableModel;
	private JTable movieTable;

	private boolean isAdminLoggedIn = false;

	public MovieAppGUI() {
		setTitle("Movie Recommendation App");
		setSize(400, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		initializeComponents();

		setLayout(new BorderLayout());

		// Add an empty JScrollPane for the JTable during initialization
		JScrollPane emptyScrollPane = new JScrollPane();
		add(emptyScrollPane, BorderLayout.CENTER);
		emptyScrollPane.setVisible(true);
		// ... (other GUI settings)
	}

	private void initializeComponents() {
		movieTypeField = new JTextField(15);
		searchButton = new JButton("Search Movies");

		searchButton.addActionListener(e -> searchMovies());

		filmTypeComboBox = new JComboBox<>(new String[]{"Action", "Comedy", "Sci-Fi"});
		loginButton = new JButton("Login as Admin");

		loginButton.addActionListener(e -> toggleAdminLogin());

		JPanel inputPanel = new JPanel();
		inputPanel.add(new JLabel("Film Type: "));
		inputPanel.add(filmTypeComboBox);
		inputPanel.add(new JLabel("Search by Movie Type: "));
		inputPanel.add(movieTypeField);
		inputPanel.add(searchButton);

		JPanel adminPanel = new JPanel();
		adminPanel.add(loginButton);

		JPanel controlPanel = new JPanel(new BorderLayout());
		controlPanel.add(inputPanel, BorderLayout.NORTH);
		controlPanel.add(adminPanel, BorderLayout.SOUTH);

		add(controlPanel, BorderLayout.NORTH);
	}

	private void addTableToUI() {
		// Remove all components from the content pane
		getContentPane().removeAll();

		// Add an empty JScrollPane for the JTable
		JScrollPane emptyScrollPane = new JScrollPane();
		add(emptyScrollPane, BorderLayout.CENTER);

		// If the JTable exists, add it to the JScrollPane
		if (movieTable != null) {
			JScrollPane scrollPane = new JScrollPane(movieTable);
			add(scrollPane, BorderLayout.CENTER);
		}

		// Add the control panel back to the frame
		initializeComponents();

		// Refresh the UI
		revalidate();
		repaint();
	}

	private void searchMovies() {
		try {
			// Get the database connection
			Connection connection = DatabaseConnection.getConnection();

			// Use the connection to query the database
			String selectedType = isAdminLoggedIn ? filmTypeComboBox.getSelectedItem().toString() : movieTypeField.getText();
			String query = "SELECT * FROM movies_info WHERE type = ?";
			try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
				preparedStatement.setString(1, selectedType);

				ResultSet resultSet = preparedStatement.executeQuery();

				// Clear existing rows in the table model
				if (tableModel != null) {
					tableModel.setRowCount(0);
				}

				// Process the result set and add rows to the table model
				Vector<Vector<Object>> data = new Vector<>();
				while (resultSet.next()) {
					Vector<Object> row = new Vector<>();
					row.add(resultSet.getString("movie_name"));
					row.add(resultSet.getString("type"));
					row.add(resultSet.getDouble("rating"));
					data.add(row);
				}

				// Create JTable and set the data
				String[] columnNames = {"Movie Name", "Type", "Rating"};
				tableModel = new DefaultTableModel(data, new Vector<>(Arrays.asList(columnNames)));
				movieTable = new JTable(tableModel);

				// Add the JTable to the UI
				addTableToUI();
			} catch (SQLException ex) {
				ex.printStackTrace();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void toggleAdminLogin() {
		if (!isAdminLoggedIn) {
			isAdminLoggedIn = true;
			loginButton.setText("Logout");
			filmTypeComboBox.setEnabled(true);
		} else {
			isAdminLoggedIn = false;
			loginButton.setText("Login as Admin");
			filmTypeComboBox.setEnabled(false);
		}

		// Add the JTable to the UI after login/logout
		addTableToUI();
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(() -> {
			MovieAppGUI movieAppGUI = new MovieAppGUI();
			movieAppGUI.setVisible(true);
		});
	}
}
